import java.io.*;
import java.rmi.*;
import java.util.*;

class NoName 
{
  private int HOST_COUNT;

  private double matrixA [][];  private double matrixA1[][]; 
  private double matrixA2[][];  private double matrixA3[][];
  
  private double matrixB [][];  private double matrixC [][];

  private double matrixCZ[][];  private double matrixCZ1[][]; 
  private double matrixCZ2[][]; private double matrixCZ3[][];
  
  private double matrixZ[][];   private double matrixZ1[][]; 
  private double matrixZ2[][];  private double matrixZ3[][];
  private int bad_hosts=0;
  private int d1,d2,d3;
	  
NoName(double matrixB[][],double matrixC[][], double matrixZ[][], String [] adress) throws Exception
  { d1=d2=d3=1;
  matrixA = new double[matrixB.length][matrixZ[0].length];
  bad_hosts = HOST_COUNT = matrixZ[0].length;
O [] host = new O[HOST_COUNT];
try{
for(int i=0;i<HOST_COUNT;i++)
	host[i] = (O)Naming.lookup("rmi://"+adress[i]+"/OServer");   
}catch(Exception e){System.out.println("RMI Exception: "+e);}

if(bad_hosts==1)
{	  
     matrixCZ=new double[matrixC.length][d1];
 try{  matrixCZ=host[0].getMatrix(matrixC,matrixZ);
       matrixA =host[0].getMatrix(matrixB,matrixCZ);
}catch(Exception e){System.out.println("RMI Exception: "+e);}
}else
{ 
if(bad_hosts==2)
  { 
 matrixCZ1 = new double[matrixC.length][d1];
 matrixCZ2 = new double[matrixC.length][d2];
 matrixZ1  = new double[matrixZ.length][d1];
 matrixZ2  = new double[matrixZ.length][d2];
 matrixA1  = new double[matrixB.length][d1];
 matrixA2  = new double[matrixB.length][d2];
 for(int i=0;i<matrixZ.length;i++)
 {matrixZ1[i][0]=matrixZ[i][0]; matrixZ2[i][0]=matrixZ[i][1]; }
 try{  matrixCZ1=host[0].getMatrix(matrixC,matrixZ1);
       matrixCZ2=host[1].getMatrix(matrixC,matrixZ2);	 
       matrixA1=host[0].getMatrix(matrixB,matrixCZ1);
       matrixA2=host[1].getMatrix(matrixB,matrixCZ2);
      for(int i=0;i<matrixA.length;i++) 
 { matrixA[i][0]=matrixA1[i][0]; matrixA[i][1]=matrixA2[i][0]; }
}catch(Exception e){System.out.println("RMI Exception: "+e);}
  }//  3 bad hosts
  else { 
 matrixCZ1 = new double[matrixC.length][d1];
 matrixCZ2 = new double[matrixC.length][d2];
 matrixCZ3 = new double[matrixC.length][d3];
 matrixZ1  = new double[matrixZ.length][d1];
 matrixZ2  = new double[matrixZ.length][d2];
 matrixZ3  = new double[matrixZ.length][d3];
 matrixA1  = new double[matrixB.length][d1];
 matrixA2  = new double[matrixB.length][d2];
 matrixA3  = new double[matrixB.length][d3];
 for(int i=0;i<matrixZ.length;i++)
 {matrixZ1[i][0]=matrixZ[i][0]; 
  matrixZ2[i][0]=matrixZ[i][1]; 
  matrixZ3[i][0]=matrixZ[i][2];}
 try{  matrixCZ1=host[0].getMatrix(matrixC,matrixZ1);
       matrixCZ2=host[1].getMatrix(matrixC,matrixZ2);	 
       matrixCZ3=host[2].getMatrix(matrixC,matrixZ3);	 
       matrixA1=host[0].getMatrix(matrixB,matrixCZ1);
       matrixA2=host[1].getMatrix(matrixB,matrixCZ2);
       matrixA3=host[2].getMatrix(matrixB,matrixCZ3);
      for(int i=0;i<matrixA.length;i++) 
 { matrixA[i][0]=matrixA1[i][0]; matrixA[i][1]=matrixA2[i][0]; 
   matrixA[i][2]=matrixA3[i][0];}
  }catch(Exception e){System.out.println("RMI Exception: "+e);}
  }

  FileWriter fw = new FileWriter("result.txt",false);
for(int i=0;i<matrixA.length;i++)
{  for(int j=0;j<matrixA[0].length;j++)
  fw.write(matrixA[i][j]+" ");
  fw.write("\n");
}
  fw.close();
  System.exit(0);
  }
}
}
